<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="资源名称" v-model="listQuery.name" clearable> </el-input>
      <el-button class="filter-item" type="primary"  icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>
<el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row>
  <el-table-column align="center" label="id" >
      <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
      </template>
  </el-table-column>

 <el-table-column  align="center" label="资源名称">
   <template slot-scope="scope">
    <span>{{scope.row.name}}</span>
  </template>
</el-table-column>

<el-table-column  align="center" label="资源类型">
  <template slot-scope="scope">
    <span>{{scope.row.type === 2?'按钮':'未知'}}</span>
  </template>
</el-table-column>

<el-table-column  align="center" width="180px" label="资源编码"><template slot-scope="scope">
  <span>
    {{scope.row.perm}}</span>
</template>
</el-table-column>
<el-table-column  align="center" width="180px" label="资源路径"><template slot-scope="scope">
  <span>
    {{scope.row.path}}</span>
</template>
</el-table-column>
<el-table-column  align="center" width="180px" label="资源状态">
  <template slot-scope="scope">
      <span v-if="scope.row.enable" style="color:green">
                  启用
      </span>
      <span v-else style="color:red">
                  禁用
    </span>
  </template>
</el-table-column>
<el-table-column   width="200px" align="center" label="创建时间"><template slot-scope="scope">
  <span>
    {{scope.row.insertDt}}</span>
</template>
</el-table-column >
<!-- <el-table-column   width="120px" align="center" label="最新操作用户"><template slot-scope="scope">
  <span>
    {{scope.row.updtUserId}}</span>
</template>
</el-table-column > -->
<el-table-column  width="200px"  align="center" label="操作" fixed="right"><template slot-scope="scope">
  <el-button  size="small" type="success" @click="handleUpdate(scope.row)">
    编辑
  </el-button>
  <el-button  size="small" type="danger" @click="handleDelete(scope.row)">删除
  </el-button>
</template>

</el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.currentPage" :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"　center width="30%">
    <el-form :model="form" :rules="rules" ref="btnform" label-width="100px">
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入资源名称"></el-input>
      </el-form-item>
      <el-form-item label="资源类型" prop="type">
         <el-select class="filter-item" v-model="form.type" placeholder="请输入资源类型">
          <el-option v-for="item in  typeOptions" :key="item.key" :label="item.value" :value="item.key"> </el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="资源编码" prop="perm">
        <el-input v-model="form.perm" placeholder="请输入资源编码"></el-input>
      </el-form-item>
       <el-form-item label="资源路径" prop="path">
        <el-input v-model="form.path" placeholder="请输入资源路径"></el-input>
      </el-form-item>
       <el-form-item label="状态" prop="enable">
          <el-radio-group v-model="form.enable">
                <el-radio class="radio"  :label="1">启用</el-radio>
                <el-radio class="radio"  :label="0">禁用</el-radio>
          </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('btnform')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('btnform')">确 定</el-button>
      <el-button v-else type="primary" @click="update('btnform')">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getMenuBtn, delmenuRequest,menuInsert ,menuUpdate } from '@/api/sys/menu'
export default {
  name: 'btnTable',
  data() {
    return {
      methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
      typeOptions: [
        {
          key:2,
          value:'按钮'
        }
      ],
      form: {
        name: undefined,
        type: undefined,
        path: undefined,
        perm:undefined,
        parentId: this.menuId,
        enable: true
      },
      rules: {
         name: [{
          required: true,
          message: '请输入资源名称',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 20,
          message: '长度在 2 到 20 个字符',
          trigger: 'blur'
        }
        ],

        type: [{
          required: true,
          message: '请选择资源类型',
          trigger: 'blur'
        }
        ],
        perm: [{
          required: true,
          message: '请输入资源编码',
          trigger: 'blur'
        }
        ],
        path: [{
          required: true,
          message: '请输入资源路径',
          trigger: 'blur'
        }
        ]
      },
      list: null,
      total: null,
      listLoading: false,
      listQuery: {
        currentPage: 1,
        size: 20,
        name: undefined,
        parentId: this.menuId
      },
      dialogFormVisible: false,
      dialogStatus: '',
      menuId: -1,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList() {

      if(this.menuId === -1){
        return
      }
      this.listLoading = true;
      this.listQuery.parentId = this.menuId;
      getMenuBtn(this.listQuery).then((response) => {
         this.list = response.data.records
         this.total = response.data.total
         this.listLoading = false
      })
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.currentPage = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      // getObj(row.id).then(response => {
      //   this.form = response.data;
        this.form = JSON.parse(JSON.stringify(row))
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
      // });
    },
    handleDelete(row) {
      const data = JSON.parse(JSON.stringify(row))
      this.$confirm('此操作将删除按钮, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delmenuRequest(data).then((response) => {
          if(response.success){
            this.getList();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }
        })
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
      })
    },
    create(formName) {
      const set = this.$refs;
      this.form.parentId = this.menuId;
      set[formName].validate(valid => {
        if (valid) {
          menuInsert(this.form).then((response) => {
          if(response.success){
            this.dialogFormVisible = false;
            this.getList();
            this.$message({
              message: '添加成功',
              type: 'success'
            });
          }
          })
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      const set = this.$refs;
      set[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      this.form.parentId = this.menuId;
      set[formName].validate(valid => {
        if (valid) {
          menuUpdate(this.form).then((response) => {
            if(response.success){
              this.dialogFormVisible = false;
              this.getList();
              this.$message({
                message: '更新成功',
                type: 'success'
              });
            }
          })
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.form = {
        name: undefined,
        type: undefined,
        path: undefined,
        parentId: undefined,
        enable: true
      };
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.filter-container{
  margin: 5px 0px;
}
</style>
